import relationalStore from '@ohos.data.relationalStore';
import { FilterPredicatesModel } from '../model/FilterPredicatesModel';
import dataPreferences from '@ohos.data.preferences';
export class DbUtil {
    getStoreConfig() {
        return this.mStoreConfig;
    }
    constructor() {
        this.mConflictResolution = relationalStore.ConflictResolution.ON_CONFLICT_NONE;
        this.mDbName = "abner_data.db";
        this.mDistributedType = relationalStore.DistributedType.DISTRIBUTED_CLOUD;
        this.mSubscribeType = relationalStore.SubscribeType.SUBSCRIBE_TYPE_REMOTE;
    }
    static getInstance() {
        if (!DbUtil.mDbUtil) {
            DbUtil.mDbUtil = new DbUtil();
        }
        DbUtil.mDbUtil.mFilterPredicatesModel = new FilterPredicatesModel();
        return DbUtil.mDbUtil;
    }
    init(d14, e14) {
        this.mStoreConfig = this.setStoreConfig(e14);
        relationalStore.getRdbStore(d14, this.mStoreConfig, (g14, h14) => {
            if (g14) {
                console.error(`Get RdbStore failed, code is ${g14.code},message is ${g14.message}`);
                return;
            }
            this.mRdbStore = h14;
        });
        this.initPreferences(d14, "db_config");
    }
    initPreferences(x13, y13) {
        let z13 = { name: y13 };
        dataPreferences.getPreferences(x13, z13, (b14, c14) => {
            if (b14) {
                console.error("Failed to get preferences. code =" + b14.code + ", message =" + b14.message);
                return;
            }
            this.dbConfig = c14;
        });
    }
    getRdbStore() {
        return this.mRdbStore;
    }
    setStoreConfig(v13) {
        let w13 = {
            name: this.mDbName,
            securityLevel: relationalStore.SecurityLevel.S3,
            encrypt: true
        };
        if (v13 != undefined) {
            if (v13.name != undefined) {
                w13.name = v13.name;
                this.mDbName = v13.name;
            }
            if (v13.securityLevel != undefined) {
                w13.securityLevel = v13.securityLevel;
            }
            if (v13.encrypt != undefined) {
                w13.encrypt = v13.encrypt;
            }
            if (v13.dataGroupId != undefined) {
                w13.dataGroupId = v13.dataGroupId;
            }
            if (v13.customDir != undefined) {
                w13.customDir = v13.customDir;
            }
            if (v13.autoCleanDirtyData != undefined) {
                w13.autoCleanDirtyData = v13.autoCleanDirtyData;
            }
        }
        return w13;
    }
    getDbName() {
        return this.mStoreConfig?.name;
    }
    getDbCustomDir() {
        return this.mStoreConfig?.customDir;
    }
    deleteRdbStore(r13, s13) {
        relationalStore.deleteRdbStore(r13, this.mStoreConfig, (u13) => {
            if (u13) {
                if (s13 != undefined) {
                    s13(false, u13);
                }
                return;
            }
            this.mRdbStore = undefined;
            if (s13 != undefined) {
                s13(true);
            }
        });
    }
    setDbVersion(q13) {
        if (this.isRdbStoreEmpty()) {
            return;
        }
        this.mRdbStore.version = q13;
    }
    isRdbStoreEmpty() {
        if (this.mRdbStore == undefined) {
            console.error("ERROR:是不是忘记了init初始化方法！");
            return true;
        }
        return false;
    }
    executeSql(g13, h13) {
        if (this.isRdbStoreEmpty()) {
            return;
        }
        if ((g13.indexOf("CREATE") == -1 && g13.indexOf("create") == -1)) {
            this.mRdbStore?.executeSql(g13, (p13) => {
                if (p13) {
                    if (h13 != undefined) {
                        h13(false, p13);
                    }
                    return;
                }
                if (h13 != undefined) {
                    h13(true);
                }
            });
            return;
        }
        let i13 = this.getSqlName(g13);
        if (i13 != undefined) {
            this.isDbTableExist(i13, (k13) => {
                if (k13) {
                    console.error("Error:当前创建的表已经存在");
                    if (h13 != undefined) {
                        h13(false, { code: -1, message: "当前创建的表已经存在", name: "Error" });
                    }
                    return;
                }
                this.mRdbStore?.executeSql(g13, (m13) => {
                    if (m13) {
                        if (h13 != undefined) {
                            h13(false, m13);
                        }
                        return;
                    }
                    let n13 = this.getSqlName(g13);
                    if (n13 != undefined) {
                        this.dbConfig?.put(n13, "数据库");
                        this.dbConfig?.flush();
                    }
                    if (h13 != undefined) {
                        h13(true);
                    }
                });
            });
        }
    }
    getSqlName(c13) {
        if ((c13.indexOf("CREATE") != -1 || c13.indexOf("create") != -1) && c13.indexOf("(") != -1) {
            let d13 = c13.split("(")[0];
            let e13 = d13.split(" ");
            let f13 = e13[e13.length - 1];
            return f13;
        }
        return undefined;
    }
    isDbTableExist(z12, a13) {
        let b13 = this.dbConfig?.hasSync(z12);
        if (a13 != undefined) {
            if (b13 != undefined) {
                a13(b13);
            }
            else {
                a13(false);
            }
        }
    }
    changeConflictResolution(y12) {
        this.mConflictResolution = y12;
        return this;
    }
    insert(p12, q12, r12, s12) {
        this.isRdbStoreEmpty();
        this.isDbTableExist(p12, (u12) => {
            if (u12) {
                this.mRdbStore?.insert(p12, q12, this.mConflictResolution, (w12, x12) => {
                    if (w12) {
                        if (s12 != undefined) {
                            s12(w12);
                        }
                        return;
                    }
                    if (r12 != undefined) {
                        r12(x12);
                    }
                });
            }
            else {
                console.error("Error:当前创建的表不存在");
                if (s12 != undefined) {
                    s12({ code: -1, message: "当前创建的表不存在", name: "Error" });
                }
            }
        });
        return this;
    }
    insertPromise(n12, o12) {
        this.isRdbStoreEmpty();
        return this.mRdbStore.insert(n12, o12, this.mConflictResolution);
    }
    batchInsert(g12, h12, i12, j12) {
        if (this.isRdbStoreEmpty()) {
            return;
        }
        this.mRdbStore?.batchInsert(g12, h12, (l12, m12) => {
            if (l12) {
                if (j12 != undefined) {
                    j12(l12);
                }
                return;
            }
            if (i12 != undefined) {
                i12(m12);
            }
        });
    }
    batchInsertPromise(e12, f12) {
        this.isRdbStoreEmpty();
        return this.mRdbStore.batchInsert(e12, f12);
    }
    getRdbPredicates(d12) {
        return new relationalStore.RdbPredicates(d12);
    }
    update(w11, x11, y11, z11) {
        if (this.isRdbStoreEmpty()) {
            return;
        }
        this.mRdbStore?.update(x11, w11, this.mConflictResolution, (b12, c12) => {
            if (b12) {
                if (z11 != undefined) {
                    z11(b12);
                }
                return;
            }
            if (y11 != undefined) {
                y11(c12);
            }
        });
    }
    updatePromise(u11, v11) {
        this.isRdbStoreEmpty();
        return this.mRdbStore.update(v11, u11);
    }
    delete(o11, p11, q11) {
        if (this.isRdbStoreEmpty()) {
            return;
        }
        this.mRdbStore?.delete(o11, (s11, t11) => {
            if (s11) {
                if (q11 != undefined) {
                    q11(s11);
                }
                return;
            }
            if (p11 != undefined) {
                p11(t11);
            }
        });
    }
    deletePromise(n11) {
        this.isRdbStoreEmpty();
        return this.mRdbStore.delete(n11);
    }
    queryAll(k11, l11, m11) {
        this.query(new relationalStore.RdbPredicates(k11), l11, m11);
    }
    query(e11, f11, g11) {
        if (this.isRdbStoreEmpty()) {
            return;
        }
        this.filterPredicates(e11);
        this.mRdbStore?.query(e11, (i11, j11) => {
            if (i11) {
                if (g11 != undefined) {
                    g11(i11);
                }
                return;
            }
            if (f11 != undefined) {
                f11(j11);
            }
            j11.close();
        });
    }
    querySql(y10, z10, a11) {
        if (this.isRdbStoreEmpty()) {
            return;
        }
        this.mRdbStore?.querySql(y10, (c11, d11) => {
            if (c11) {
                if (a11 != undefined) {
                    a11(c11);
                }
                return;
            }
            z10(d11);
            d11.close();
        });
    }
    getModifyTime(q10, r10, s10, t10, u10) {
        if (this.isRdbStoreEmpty()) {
            return;
        }
        this.mRdbStore?.getModifyTime(q10, r10, s10, (w10, x10) => {
            if (w10) {
                if (u10 != undefined) {
                    u10(w10);
                }
                return;
            }
            t10(x10);
        });
    }
    beginTransaction() {
        if (this.isRdbStoreEmpty()) {
            return;
        }
        this.mRdbStore?.beginTransaction();
    }
    commit() {
        if (this.isRdbStoreEmpty()) {
            return;
        }
        this.mRdbStore?.commit();
    }
    rollBack() {
        if (this.isRdbStoreEmpty()) {
            return;
        }
        this.mRdbStore?.rollBack();
    }
    backup(l10, m10, n10) {
        if (this.isRdbStoreEmpty()) {
            return;
        }
        this.mRdbStore?.backup(l10, (p10) => {
            if (p10) {
                if (n10 != undefined) {
                    n10(p10);
                }
                return;
            }
            if (m10 != undefined) {
                m10();
            }
        });
    }
    restore(g10, h10, i10) {
        if (this.isRdbStoreEmpty()) {
            return;
        }
        this.mRdbStore?.restore(g10, (k10) => {
            if (k10) {
                if (i10 != undefined) {
                    i10(k10);
                }
                return;
            }
            if (h10 != undefined) {
                h10();
            }
        });
    }
    setDistributedType(f10) {
        this.mDistributedType = f10;
        return this;
    }
    setDistributedTables(a10, b10, c10) {
        if (this.isRdbStoreEmpty()) {
            return;
        }
        this.mRdbStore?.setDistributedTables(a10, this.mDistributedType, (e10) => {
            if (e10) {
                if (c10 != undefined) {
                    c10(e10);
                }
                return;
            }
            if (b10 != undefined) {
                b10();
            }
        });
    }
    setSubscribeType(z9) {
        this.mSubscribeType = z9;
        return this;
    }
    onDataChange(y9) {
        if (this.isRdbStoreEmpty()) {
            return;
        }
        this.mRdbStore?.on('dataChange', this.mSubscribeType, y9);
    }
    offDataChange(x9) {
        if (this.isRdbStoreEmpty()) {
            return;
        }
        this.mRdbStore?.off('dataChange', this.mSubscribeType, x9);
    }
    createTable(r9, s9) {
        let t9 = "CREATE TABLE " + r9 + "(";
        Object.keys(s9).forEach((v9) => {
            t9 = t9 + v9 + " ";
            let w9 = s9[v9];
            if (w9.length != undefined) {
                t9 = t9 + w9.type?.toString() + "(" + w9.length + ")";
            }
            else {
                t9 = t9 + w9.type?.toString();
            }
            if (w9.isPrimaryKey) {
                t9 = t9 + " PRIMARY KEY";
            }
            if (w9.isAutoIncrement) {
                t9 = t9 + " AUTOINCREMENT";
            }
            t9 = t9 + ",";
        });
        if (t9.endsWith(",")) {
            t9 = t9.substring(0, t9.length - 1);
        }
        t9 = t9 + ")";
        this.executeSql(t9);
    }
    updateTable(j9, k9) {
        this.isDbTableExist(j9, (m9) => {
            if (m9) {
                Object.keys(k9).forEach((o9) => {
                    let p9 = "ALTER TABLE " + j9 + " ADD " + o9 + " ";
                    let q9 = k9[o9];
                    if (q9.length != undefined) {
                        p9 = p9 + q9.type?.toString() + "(" + q9.length + ")";
                    }
                    else {
                        p9 = p9 + q9.type?.toString();
                    }
                    this.mRdbStore?.executeSql(p9);
                });
            }
            else {
                console.error("Error:当前创建的表不存在");
            }
        });
    }
    queryAllBean(c9, d9, e9) {
        this.queryAll(c9, (g9) => {
            const h9 = Array();
            while (g9.goToNextRow()) {
                const i9 = JSON.parse(JSON.stringify(g9.getRow()));
                h9.push(i9);
            }
            d9(h9);
        }, e9);
    }
    queryBean(v8, w8, x8) {
        let y8 = new relationalStore.RdbPredicates(v8);
        this.filterPredicates(y8);
        this.query(y8, (a9) => {
            if (!a9.goToNextRow()) {
                if (x8 != undefined) {
                    x8({
                        code: -1,
                        name: 'Error',
                        message: '当前查询的数据不存在'
                    });
                }
            }
            else {
                const b9 = JSON.parse(JSON.stringify(a9.getRow()));
                w8(b9);
            }
        }, x8);
    }
    insertBean(t8, u8) {
        this.insert(t8, u8);
    }
    deleteBean(p8, q8, r8) {
        let s8 = new relationalStore.RdbPredicates(p8);
        this.filterPredicates(s8);
        this.delete(s8, q8, r8);
    }
    updateBean(k8, l8, m8, n8) {
        let o8 = new relationalStore.RdbPredicates(k8);
        this.filterPredicates(o8);
        this.update(o8, l8, m8, n8);
    }
    filterRdbPredicates(j8) {
        this.mFilterPredicatesModel = j8;
        return this;
    }
    filterPredicates(y7) {
        if (this.mFilterPredicatesModel?.limitAs != undefined) {
            y7.limitAs(this.mFilterPredicatesModel?.limitAs);
        }
        if (this.mFilterPredicatesModel?.offsetAs != undefined) {
            y7.offsetAs(this.mFilterPredicatesModel?.offsetAs);
        }
        if (this.mFilterPredicatesModel?.orderByAsc != undefined) {
            y7.orderByAsc(this.mFilterPredicatesModel?.orderByAsc);
        }
        if (this.mFilterPredicatesModel?.orderByDesc != undefined) {
            y7.orderByDesc(this.mFilterPredicatesModel?.orderByDesc);
        }
        if (this.mFilterPredicatesModel?.equalTo != undefined) {
            Object.keys(this.mFilterPredicatesModel?.equalTo).forEach((i8) => {
                y7.equalTo(i8, this.mFilterPredicatesModel?.equalTo[i8]);
            });
        }
        if (this.mFilterPredicatesModel?.notEqualTo != undefined) {
            Object.keys(this.mFilterPredicatesModel?.notEqualTo).forEach((g8) => {
                y7.notEqualTo(g8, this.mFilterPredicatesModel?.notEqualTo[g8]);
            });
        }
        if (this.mFilterPredicatesModel?.contains != undefined) {
            Object.keys(this.mFilterPredicatesModel?.contains).forEach((e8) => {
                y7.contains(e8, this.mFilterPredicatesModel?.contains[e8]);
            });
        }
        if (this.mFilterPredicatesModel?.between != undefined) {
            Object.keys(this.mFilterPredicatesModel?.between).forEach((a8) => {
                let b8 = this.mFilterPredicatesModel?.between[a8];
                let c8 = b8?.split("=");
                if (c8?.length == 2) {
                    y7.between(a8, c8[0], c8[1]);
                }
            });
        }
    }
}
